﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using jaysJourney_v01.Common;
using jaysJourney_v01.Utils;
using System.Diagnostics;

namespace jaysJourney_v01.States
{
    class CurWorldDetailsState : State
    {
        /**
         * MEMBER VARIABLES
         * */

        // From Parent
        private SpriteBatch mainSpriteBatch;

        // Common
        private PlayState playState;
        private LoadWorldState loadWorldState;

        private Texture2D onePx;
        private int sizeField = 2;

        private Texture2D interfaceBG;

        private bool loading;
        private Texture2D loadingscreen;
        private String clickname;

        // Buttons
        private InputGroup menuButtonGroup;

        private Button play;
        private Button back;

        /**
         * METHODS
         * */

        // Constructor

        public CurWorldDetailsState(String name, StateManager stateManager)
            : base(name, stateManager)
        {

        }

        // Initialize - Called only once

        public override void initialize()
        {
            this.mainSpriteBatch = this.parentSpriteBatch;
            this.playState = (PlayState)getStateManager().getState("playState");
            this.loadWorldState = (LoadWorldState)getStateManager().getState("loadWorldState");
            this.loadingscreen = this.getGame().Content.Load<Texture2D>(@"Textures\Preloader\loadingscreen");



            this.onePx = this.getGame().Content.Load<Texture2D>(@"Textures\Messages\1x1");

            this.interfaceBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\Stage_Overview");

            // Buttons
            this.play = new Button(this, "PLAY", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", (this.getGame().GraphicsDevice.Viewport.Width / 2) + 200, 650, Color.White, true, true);
            this.back = new Button(this, "BACK", @"Textures\Interface\back_btn", @"Textures\Interface\back_btn_ac", 20, this.getGame().GraphicsDevice.Viewport.Height - 74, Color.White, false, false);

            this.menuButtonGroup = new InputGroup(this, this.mainSpriteBatch);

            this.menuButtonGroup.addButton(play);
            this.menuButtonGroup.addButton(back);
        }

        // MAIN UPDATE LOOP

        public override void update(GameTime gameTime)
        {


            //action after loadingscreen
            if (loading)
            {
                loading = false;
                switch (clickname)
                {
                    case "PLAY":
                        this.getStateManager().setActiveState("playState");
                        break;
                    case "BACK":
                        this.getStateManager().setActiveState("loadWorldState");
                        break;
                }
            }
            Button click = this.menuButtonGroup.update(gameTime);

            //trigger loadingscreen & prepare for action
            if (click != null)
            {
                loading = true;
                clickname = click.getName();
            }
        }

        // MAIN DRAW METHOD

        public override void draw(GameTime gameTime)
        {
            // bg
            this.mainSpriteBatch.Draw(this.getGame().menuBg, new Rectangle(0, 0, this.getGame().GraphicsDevice.Viewport.Width, this.getGame().GraphicsDevice.Viewport.Height), new Rectangle(0, 0, this.getGame().menuBg.Width, this.getGame().menuBg.Height), Color.White);

            // interface bg
            this.mainSpriteBatch.Draw(this.interfaceBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.interfaceBG.Width / 2, 0, this.interfaceBG.Width, this.interfaceBG.Height), new Rectangle(0, 0, this.interfaceBG.Width, this.interfaceBG.Height), Color.White);

            // buttons
            this.menuButtonGroup.draw(gameTime);

            // draw text
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "   STAGENAME:", new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 50, 70), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "     CREATOR:", new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 50, 120), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "    BESTTIME:", new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 50, 170), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "RECORDHOLDER:", new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 50, 220), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "AVG BESTTIME:", new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 50, 270), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "    PERSONAL:", new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 50, 320), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);

            // draw data
            this.mainSpriteBatch.DrawString(this.getGame().textFont, this.playState.getLvlData().getLevel(), new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 250, 70), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, this.playState.getLvlData().getCreatorName(), new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 250, 120), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, Formater.formatTime(this.playState.getLvlData().getBestTime()), new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 250, 170), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, this.playState.getLvlData().getRecordHolder(), new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 250, 220), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, Formater.formatTime(this.playState.getLvlData().getAvgBestTime()), new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 250, 270), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, Formater.formatTime(this.playState.getLvlData().getPersonalBest()), new Vector2((this.getGame().GraphicsDevice.Viewport.Width / 2) + 250, 320), Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1f);

            // draw minimap
            printMinimap();
            if (loading) this.mainSpriteBatch.Draw(this.loadingscreen, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.loadingscreen.Width / 2, (this.getGame().GraphicsDevice.Viewport.Height / 2) - this.interfaceBG.Height / 2, this.loadingscreen.Width, this.loadingscreen.Height), new Rectangle(0, 0, this.loadingscreen.Width, this.loadingscreen.Height), Color.White);

        }

        public override void onEnterState()
        {
            this.play.setNewPosition((this.getGame().GraphicsDevice.Viewport.Width / 2) + 200, 650);
            this.back.setNewPosition(20, this.getGame().GraphicsDevice.Viewport.Height - 74);
        }

        private void printMinimap()
        {
            FieldType[,] curLevel = playState.getLevel().curLevel;

            for (int x = 0; x < playState.getLevel().XCOUNT; x++)
            {
                for (int y = 0; y < playState.getLevel().YCOUNT; y++)
                {
                    switch (curLevel[x, y])
                    {
                        case FieldType.Nothing:
                            mainSpriteBatch.Draw(onePx, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - 410 + x * sizeField, 50 + y * sizeField, sizeField, sizeField), Color.AliceBlue);
                            break;
                        case FieldType.simpleBlock1:
                        case FieldType.simpleBlock2:
                        case FieldType.simpleBlock3:
                            mainSpriteBatch.Draw(onePx, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - 410 + x * sizeField, 50 + y * sizeField, sizeField, sizeField), Color.DarkGray);
                            break;
                        case FieldType.Goal:
                            mainSpriteBatch.Draw(onePx, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - 410 + x * sizeField, 50 + (y * sizeField) - sizeField, sizeField, sizeField * 2), Color.DarkGreen);
                            break;
                        case FieldType.Startpoint:
                            mainSpriteBatch.Draw(onePx, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - 410 + x * sizeField, 50 + (y * sizeField) - sizeField, sizeField, sizeField * 2), Color.Brown);
                            break;
                        case FieldType.dissolvingBlock:
                            mainSpriteBatch.Draw(onePx, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - 410 + x * sizeField, 50 + (y * sizeField) - sizeField, sizeField, sizeField * 2), Color.AntiqueWhite);
                            break;
                        case FieldType.spikeBlock:
                            mainSpriteBatch.Draw(onePx, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - 410 + x * sizeField, 50 + (y * sizeField) - sizeField, sizeField, sizeField * 2), Color.Gold);
                            break;
                    }
                }
            }
        }
    }
}
